//
//  HMMainViewController.swift
//  WeiBo
//
//  Created by Feng on 2017/6/4.
//  Copyright © 2017年 feng. All rights reserved.
//

import UIKit
import SVProgressHUD

class HMMainViewController: UITabBarController,HMTabBarDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        //创建tabBar
        let tabBar = HMTabBar()
        
        //闭包回调
        tabBar.composeCallBack = { [unowned self] in
            print("我是闭包的回调")
            print(self)
            
            //添加ComposeView
            //首先判断是否已经登录
            if !HMUserAccountViewModel.sharedUserAccount.isLogin
            {
                SVProgressHUD.showInfo(withStatus: "请先登录!!!")
                
                return
            }
            
            let composeView: HMComposeView = HMComposeView()
            
            composeView.show(targetVC: self)
        }
        
        //设置代理
        tabBar.hmDelegate = self
        
        
//        self.tabBar = tabBar
        //使用kvc 赋值
        self.setValue(tabBar, forKey: "tabBar")
        
        //添加自视图控制器2种方法
//        viewControllers = []
//        addChildViewController(<#T##childController: UIViewController##UIViewController#>)
        
        addChildViewController(childController: HMHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(childController: HMMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(childController: HMDiscoverTableViewController(), title: "搜索", imageName: "tabbar_discover")
        addChildViewController(childController: HMMeTableViewController(), title: "我的", imageName: "tabbar_profile")
        
    }

    //重写addChildViewController方法
    func addChildViewController(childController: UIViewController, title: String, imageName: String) {
        
        //设置控制器的tarbarItem的image属性
        childController.tabBarItem.image = UIImage(named: imageName)
        //设置图片的渲染方式--->原始
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.withRenderingMode(.alwaysOriginal)
        //设置控制器的tarbarItem的title属性
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 13)], for: .normal)
        
//        childController.tabBarItem.title = title
//        childController.navigationItem.title = title
        
        //这样的写法,后面的viewController.title 才能取到值
        childController.title = title
        
//        let nav = UINavigationController(rootViewController: childController)
        let nav = HMCustomNavigationController(rootViewController: childController)
        
        //添加子控制器
        self.addChildViewController(nav)
    }
    
    //代理方法
    func composeDelegateBack()
    {
        print("我是代理方法的回调")
    }
}
